package com.turman.builibrary.banner.core;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.turman.builibrary.R;
import com.turman.builibrary.banner.BBanner;
import com.turman.builibrary.banner.indicator.BCircleIndicator;
import com.turman.builibrary.banner.indicator.IBIndicator;

import java.util.List;

public class BBannerDelegate implements ViewPager.OnPageChangeListener, IBBanner {
    private Context mContext;
    private BBanner mBanner;
    private BBannerAdapter mAdapter;
    private IBIndicator mIndicator;
    private boolean mAutoPlay;
    private boolean mLoop;
    private List<? extends BBannerModel> mBannerModels;
    private ViewPager.OnPageChangeListener mOnPageChangeListener;
    private int mIntervalTime = 5000;
    private BBanner.OnBannerClickListener mOnBannerClickListener;
    private BViewPager mViewPager;
    private int mScrollDuration = -1;

    public BBannerDelegate(Context context, @NonNull BBanner banner) {
        mContext = context;
        this.mBanner = banner;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mOnPageChangeListener != null && mAdapter.getRealCount() != 0) {
            mOnPageChangeListener.onPageScrolled(position % mAdapter.getRealCount(), positionOffset, positionOffsetPixels);
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (mAdapter.getRealCount() == 0) {
            return;
        }
        position = position % mAdapter.getRealCount();
        if (mOnPageChangeListener != null) {
            mOnPageChangeListener.onPageSelected(position);
        }
        if (mIndicator != null) {
            mIndicator.onPointChange(position, mAdapter.getRealCount());
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (mOnPageChangeListener != null) {
            mOnPageChangeListener.onPageScrollStateChanged(state);
        }
    }

    @Override
    public void setBannerData(int layoutRes, @NonNull List<? extends BBannerModel> models) {
        this.mBannerModels = models;
        init(layoutRes);
    }

    private void init(@LayoutRes int layoutRes) {
        if (mAdapter == null) {
            mAdapter = new BBannerAdapter(mContext);
        }
        if (mIndicator == null) {
            //默认指示器
            mIndicator = new BCircleIndicator(mContext);
        }
        mIndicator.onInflate(mBannerModels.size());
        mAdapter.setLayoutResId(layoutRes);
        mAdapter.setBannerData(mBannerModels);
        mAdapter.setAutoPlay(mAutoPlay);
        mAdapter.setLoop(mLoop);
        mAdapter.setOnBannerClickListener(mOnBannerClickListener);

        mViewPager = new BViewPager(mContext);
        mViewPager.setInterval(mIntervalTime);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAutoPlay(mAutoPlay);
        if (mScrollDuration > 0) {
            mViewPager.setScrollDuration(mScrollDuration);
        }

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        mViewPager.setAdapter(mAdapter);

        if ((mLoop || mAutoPlay) && mAdapter.getRealCount() != 0) {
            //无限轮播关键点：使第一张能反向滑动到最后一张，已达到无限滚动的效果
            int firstItem = mAdapter.getFirstItem();
            mViewPager.setCurrentItem(firstItem, false);
        }

        //清除缓存view
        mBanner.removeAllViews();
        mBanner.addView(mViewPager, layoutParams);
        mBanner.addView(mIndicator.getLayout(), layoutParams);
    }

    @Override
    public void setBannerData(@NonNull List<? extends BBannerModel> models) {
        setBannerData(R.layout.b_banner_item_image, models);
    }

    @Override
    public void setIndicator(@NonNull IBIndicator indicator) {
        mIndicator = indicator;
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        this.mAutoPlay = autoPlay;
        if (mAdapter!= null) {
            mAdapter.setAutoPlay(mAutoPlay);
        }
        if (mViewPager!=null) {
            mViewPager.setAutoPlay(mAutoPlay);
        }
    }

    @Override
    public void setLoop(boolean loop) {
        this.mLoop = loop;
    }

    @Override
    public void setInterval(int interval) {
        if (interval>0) {
            this.mIntervalTime = interval;
        }
    }

    @Override
    public void setBindAdapter(@NonNull IBindAdapter adapter) {
        this.mAdapter.setBindAdapter(adapter);
    }

    @Override
    public void setOnPageChangeListener(@NonNull ViewPager.OnPageChangeListener onPageChangeListener) {
        this.mOnPageChangeListener = onPageChangeListener;
    }

    @Override
    public void setOnBannerChickListener(@NonNull OnBannerClickListener onBannerChickListener) {
        this.mOnBannerClickListener = onBannerChickListener;
        mAdapter.setOnBannerClickListener(mOnBannerClickListener);
    }

    @Override
    public void setScrollDuration(int duration) {
        this.mScrollDuration = duration;
        if (mViewPager != null && duration > 0) mViewPager.setScrollDuration(duration);
    }
}
